//
//  IGiGallerySetVC.m
//  jingdongfang
//
//  Created by BOE on 2017/8/23.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGiGallerySetVC.h"
#import "IGiGalleryShareView.h"
#import "IGChangeiGalleryNameVC.h"
#import "IGShareIgalleryVC.h"
#import "IGRemoteControlVC.h"
#import "IGalleryUnBindApi.h"
#import "IGRemoteControlApi.h"

@interface IGiGallerySetVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tableFootView;

@property (nonatomic, assign) IGiGalleryBindingType bindType;
@property (nonatomic, assign) NSInteger sectionNum;
@property (nonatomic, strong) IGiGalleryEqu *model;

@property (nonatomic, strong) IGRemoteControlApi *remoteApi;

@end

@implementation IGiGallerySetVC

- (instancetype)initWithType:(IGiGalleryBindingType)bindType equ:(IGiGalleryEqu *)model{
    if (self = [super init]) {
        self.bindType = bindType;
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"画框设置";
    self.title = NSLocalizedString(@"iGallery Setting", nil);

    self.view.backgroundColor = [UIColor mainBackgroundColor];
    
    if (self.bindType == IGiGalleryBindingTypeMain) {
        self.sectionNum = 3;
        self.tableView.tableFooterView = self.tableFootView;
    } else if(self.bindType == IGiGalleryBindingTypeSlave) {
        self.sectionNum = 1;
        self.tableView.tableFooterView = self.tableFootView;
    } else if(self.bindType == IGiGalleryBindingTypeSlaveWait) {
        self.sectionNum = 1;
    }
    
    self.remoteApi = [IGRemoteControlApi new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, -100, 0, 0);
    }
    return _tableView;
}

- (UIView *)tableFootView {
    if (!_tableFootView) {
        _tableFootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 100)];
        _tableFootView.backgroundColor = [UIColor mainBackgroundColor];
        UIButton *deliGalleryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [deliGalleryBtn setTitle:@"解绑设备" forState:UIControlStateNormal];
        [deliGalleryBtn setTitle:NSLocalizedString(@"Unlink", nil) forState:UIControlStateNormal];
        [deliGalleryBtn setTitleColor:[UIColor c3Color] forState:UIControlStateNormal];
        deliGalleryBtn.backgroundColor = [UIColor colorWithHex:@"#eb625a"];
        deliGalleryBtn.layer.masksToBounds = YES;
        deliGalleryBtn.layer.cornerRadius = 22;
        [_tableFootView addSubview:deliGalleryBtn];
        [deliGalleryBtn setTapActionWithBlock:^{
            [UIAlertView bk_showAlertViewWithTitle:@"" message:NSLocalizedString(@"Confirm to Unbind this Device?", nil) cancelButtonTitle:NSLocalizedString(@"Cancel",nil) otherButtonTitles:@[NSLocalizedString(@"OK",nil)] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if (buttonIndex == 1) {
                    IGalleryUnBindApi *api = [[IGalleryUnBindApi alloc] initWithMacId:self.model.macId type:self.model.type];
                    [api startWithDecodeClass:[IGalleryUnBindResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                        if (responseObject) {
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    }];
                }
            }];
        }];
        [deliGalleryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(_tableFootView.mas_centerX);
            make.width.mas_equalTo(KSCREENWIDTH*2/3);
            make.bottom.mas_equalTo(-20);
            make.height.mas_equalTo(44);
        }];
        
    }
    
    return _tableFootView;
}

#pragma mark -<UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionNum;
}

/*
 > modify by ly
 > 2018.1.17 二期 海信合作 海信设备不允许出现远程控制
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    } else if (section == 1) {
        return 1;
    } else if (section == 2) {
        if ([self.model.group isEqualToString:@"1"]) {//只有我们自己的画屏显示
            return 2;
        } else {
            return 1;
        }
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"IGiGallerySetVCCellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    
    cell.textLabel.font = [UIFont font16];
    cell.textLabel.textColor = [UIColor c4Color];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
//            cell.textLabel.text = @"设备ID";
            cell.textLabel.text = NSLocalizedString(@"Device ID", nil);

            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
            UILabel *idLabel = [UILabel new];
            idLabel.text = self.model.macId;
            idLabel.textColor = [UIColor c5Color];
            idLabel.font = [UIFont font16];
            idLabel.textAlignment = NSTextAlignmentRight;
            [view addSubview:idLabel];
            
            UIImageView *imgView = [UIImageView new];
            imgView.image = [UIImage imageNamed:@"iGallery_TwoCode"];
            [view addSubview:imgView];
            [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.right.bottom.mas_equalTo(0);
                make.width.mas_equalTo(30);
            }];
            
            [idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(imgView.mas_left).offset(-3);
                make.left.top.bottom.mas_equalTo(0);
            }];

            cell.accessoryView = view;
        } else if (indexPath.row == 1) {
//            cell.textLabel.text = @"设备名称";
            cell.textLabel.text = NSLocalizedString(@"Device Name", nil);
            cell.detailTextLabel.text = self.model.title;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
//            cell.textLabel.text = @"分享设备";
            cell.textLabel.text = NSLocalizedString(@"Sharing Device", nil);

//            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@个分享人", self.model.slaveNum];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@", self.model.slaveNum,NSLocalizedString(@"Sharers", nil)];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
//            cell.textLabel.text = @"订阅官方推送";
            cell.textLabel.text = NSLocalizedString(@"Subscribe Official Content", nil);
            UISwitch *switchV = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
            switchV.on = [self.model.pushType integerValue]==1?YES:NO;
            [switchV addTarget:self action:@selector(switChangeValueAction:) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = switchV;
        } else if (indexPath.row == 1) {
//            cell.textLabel.text = @"远程控制";
            cell.textLabel.text = NSLocalizedString(@"Remote Control", nil);
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if (self.bindType == IGiGalleryBindingTypeMain) {
                [IGiGalleryShareView showIgalleryCard:self.model];
            }
        } else if (indexPath.row == 1) {
            IGChangeiGalleryNameVC *vc = [[IGChangeiGalleryNameVC alloc] initWithModel:self.model];
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    } else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            IGShareIgalleryVC *vc = [[IGShareIgalleryVC alloc] init];
            vc.equModel = self.model;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            
        } else if (indexPath.row == 1) {//远程控制
            IGRemoteControlVC *remoteVC = [[IGRemoteControlVC alloc] initWithMacId:self.model.macId];
            [UIViewController ig_pushViewController:remoteVC animated:YES];
        }
    }
}

//开关订阅官方推送
- (void)switChangeValueAction:(UISwitch *)switchV {
    self.remoteApi.receivePush = switchV.on?@"1":@"2";
    self.remoteApi.macId = self.model.macId;
    [self.remoteApi startWithDecodeClass:[IGRemoteControlResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (!responseObject) {
            switchV.on = !switchV.on;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
